package app_res

import (
	"errors"
	"fmt"
	"hft/Database/admin"
	"hft/Http/Services/front/app/app_dao"
	"hft/Http/Services/front/app/app_model"
	"hft/e"
	"hft/global"
	"hft/tools"
)

// 移动端
type Application struct{}

// 获取首页
func (*Application) Index(city int) (data *app_model.Index, err error) {
	regionLink, err := app_dao.AppBaseDao.GetRegion(global.OC_DB, city)
	if err != nil {
		return nil, err
	}
	newlist := app_dao.AppIndexDao.GetArticle(global.OC_DB, regionLink)      //今日资讯
	recommend := app_dao.AppIndexDao.GetLouPanList(global.OC_DB, regionLink) //推荐楼盘
	newLou := app_dao.AppIndexDao.GetLouPanNewList(global.OC_DB, regionLink) //新房
	emojiList := app_dao.AppIndexDao.GetEmojiList(global.OC_DB)
	var index = &app_model.Index{
		News:            newlist,
		RecommendLouPan: recommend,
		NewLouPan:       newLou,
		Emoji:           emojiList,
	}
	return index, nil
}

/* 换取城市id */
func (*Application) TransSiteID(city_name string) (*app_model.TransSID, error) {
	var transCity = &app_model.TransSID{Sid: 0, Name: ""}
	var region *admin.Region
	err := global.OC_DB.Select("name,id,alias").Where("name = ?", city_name).Or("alias = ?", city_name).First(&region).Error
	fmt.Println(tools.JsonParse(region))
	if err != nil {
		return nil, errors.New(e.RegionNotFound)
	}
	transCity.Sid = int(region.ID)
	if region.Alias == "" {
		transCity.Name = region.Name
	} else {
		transCity.Name = region.Alias
	}
	fmt.Println("1111", transCity)
	return transCity, nil
}

var ApplicationService = new(Application)
